package work;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

/**
 * @auther Macrydy
 * @date 2020/7/16 18:26
 */
public class TcpServer implements Runnable {

    private Socket accept;

    public TcpServer(Socket accept) {
        this.accept = accept;
    }

    @Override
    public void run() {

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(accept.getInputStream());

            bos = new BufferedOutputStream(new FileOutputStream("F:\\" + UUID.randomUUID() + ".jpg"));

            int len;
            while ((len = bis.read()) != -1) {
                bos.write(len);
            }

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bw.write("上传成功");
            bw.newLine();
            bw.flush();
            bw.close();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bos.close();
                bis.close();
                accept.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
